<?php
/**
 * Created by PhpStorm.
 * User: liuhongchuan
 * Date: 2018/4/26
 * Time: 14:39
 *
 * 门店超级管理员管理管理
 */
namespace app\operation\controller;

use app\common\controller\OperationBase;
use think\Db;

class ShopAdmin extends OperationBase {

	function _initialize() {
		parent::_initialize();
    }

    /**
     * 门店管理员列表
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
	function index() {
        $auth_group = sp_get_auth_menu_3("20001001");
        $shops = db("shop")->where(array("parent_id" => array("neq", 0)))->order("parent_id desc, shop_name desc")->column("id, shop_name");
        $this->assign("shops", $shops);
        $this->assign("menus", $auth_group);
        echo $this->fetch();
	}

    /**
     * 删除门店管理员
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
	function delete() {
		$id = input("id/d");
		if (empty($id)) {
		    $this->ajax_error("参数错误");
        }
		$del_res = db("admin")->where(array("id" => $id))->delete();
		if (empty($del_res)) {
		    $this->ajax_error("失败");
        }
		$this->ajax_success("成功");
	}

    /**
     * 管理员列表数据
     * @return array
     */
	public function ajaxIndex() {
	    $shop_id = input("shop_id/d");
	    $account = input("account/s");
	    $where = array("a.shop_id" => array("gt", 0), "a.is_shop_admin" => array("eq", 1));
	    if (!empty($shop_id)) {
	        $where["a.shop_id"] = array("eq", $shop_id);
        }
        if (!empty($account)) {
	        $where["a.account"] = array("like", "%{$account}%");
        }

        $draw = input("draw",0) + 1;//请求时间

        $total = db("admin")->alias('a')->where($where)->count(1);
        $list = db("admin")->alias("a")
            ->field("a.*, s.shop_name")
            ->join("m_shop s","a.shop_id = s.id","left")
            ->where($where)
            ->order($this->get_order())
            ->limit($this->get_limit())
            ->select();
        if (!empty($list)) {
            $admin_ids = array_column($list, "id");

            $admin_log_ids = db("admin_log")->where(array("admin_id" => array("in", $admin_ids)))->group("admin_id")->field("max(id) as id")->select();
            $admin_log_ids = array_column($admin_log_ids, "id");
            $admin_logs = db("admin_log")->where(array("id" => array("in", $admin_log_ids)))->column("admin_id,create_time,ip");
            foreach ($list as $admin_k => $admin) {
                if (!empty($admin_logs[$admin["id"]])) {
                    $list[$admin_k]["login_time"] = $admin_logs[$admin["id"]]["create_time"];
                    $list[$admin_k]["ip"] = $admin_logs[$admin["id"]]["ip"];
                } else {
                    $list[$admin_k]["login_time"] = 0;
                    $list[$admin_k]["ip"] = 0;
                }

            }
        }

        $this->ajax(array('pageData' => $list, 'total' => $total, "draw" => $draw));
	}

    /**
     * 添加
     */
	public function add() {
	    $shops = db("shop")->where(array("parent_id" => array("gt", 0)))->column("id,shop_name");
		$this->assign("shops", $shops);
		echo $this->fetch();
	}

    /**
     * 添加操作
     */
    public function addPost(){
        if(request()->isPost()){
            $account = input("account/s");
            $name = input("name/s");
            $password = input("password/s");
            $mobile = input("mobile/s");
            $shop_id = input("shop_id");
            if (empty($account)) {
                $this->ajax_error("账号不能为空");
            }
            if (empty($name)) {
                $this->ajax_error("用户名不能为空");
            }
            if (empty($password)) {
                $this->ajax_error("密码不能为空");
            }
            if (empty($shop_id)) {
                $this->ajax_error("店铺不能为空");
            }
            $is_exist = db("admin")->where(array("account" => array("eq", $account)))->count(1);
            if ($is_exist > 0) {
                $this->ajax_error("门店超级管理员账号已经存在");
            }
            $add_res = db("admin")->insert(array("name" => $name, "account" => $account, "password" => md5($password), "mobile" => $mobile, 'shop_id' => $shop_id,"is_shop_admin" => 1));
            if (empty($add_res)) {
                $this->ajax_error("添加失败");
            }
            $this->ajax_success("成功");
        }
    }

    /**
     * 重置密码
     */
	public function resetPsw(){
		$id = input("id/d");
		$update_res = db("admin")->where(array("id" => array("eq", $id)))->setField(array("password" => md5("123456")));
		if ($update_res === false) {
		    $this->ajax_error("失败");
        }
        $this->ajax_success("成功");
	}
	
}
